package my.util;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Array;
import java.net.URI;
import java.util.Collection;
import java.util.Map;

public class Utils {
    /**
     * 去掉一个url中多余的/
     * @param pURI
     * @return
     */
    public static String normalizePath(String pURI) {
        if (StringUtils.isBlank(pURI)) {
            return pURI;
        }
        String lPath = pURI;
        try {
            lPath = URI.create(pURI).normalize().toString();
        } catch (Exception e) {
        }
        return lPath;
    }


    public static int toInt(final Object obj, final int defaultValue) {
        if(obj == null) return defaultValue;
        if (obj instanceof Integer) return (Integer) obj;
        else if (obj instanceof String) {
            try {
                return Integer.parseInt(obj.toString());
            } catch (final NumberFormatException nfe) {
                return defaultValue;
            }
        }
        return defaultValue;
    }


    public static boolean isEmpty(Collection<?> pCollection) {
        return (pCollection == null || pCollection.isEmpty());
    }


    public static boolean isNotEmpty(Collection<?> pCollection) {
        return (!isEmpty(pCollection));
    }


    public static boolean isEmpty(Object[] array) {
        return (array == null || Array.getLength(array) == 0);
    }


    public static boolean isNotEmpty(Object[] array) {
        return !isEmpty(array);
    }


    public static boolean isEmpty(final Map<?, ?> map) {
        return map == null || map.isEmpty();
    }
}
